package com.yanqu.road.web.controller.usercenter.newadmin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.admin.AdminCommonDaoImpl;
import com.yanqu.road.entity.admin.AdminResult;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.servercenter.whitelist.WhiteAccount;
import com.yanqu.road.server.manger.PlatformUserIdMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.string.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author : Cxp
 * @Time : 2024-12-30
 * @Description : 37.账号白名单
 */

@RestController
@RequestMapping("/usercenter/admin/account/whitelist/")
public class CenterAdminAccountWhiteListController {

    /**
     * 37.1 新增
     */
    @ResponseBody
    @RequestMapping(value = "add", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String add(HttpServletRequest request, @RequestBody JSONObject param) {
        String platformUserId = param.getString("openId");
        String remark = param.getString("remark");

        if (StringUtils.isNullOrEmpty(platformUserId)) {
            return AdminResult.failMsg("platformUserId 不能为空").toJsonString();
        }

        List<WhiteAccount> accounts = new ArrayList<>();
        accounts.add(new WhiteAccount(platformUserId, remark));

        return new AdminCommonDaoImpl().batchInsertAccountWhiteList(accounts) ?
                AdminResult.defaultInstance().toJsonString() : AdminResult.failMsg("失败").toJsonString();
    }

    /**
     * 37.2 编辑
     */
    @ResponseBody
    @RequestMapping(value = "edit", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String edit(HttpServletRequest request, @RequestBody JSONObject param) {
        int id = param.getIntValue("id");
        String platformUserId = param.getString("openId");
        String remark = param.getString("remark");

        if (id == 0) {
            return AdminResult.failMsg("id值错误").toJsonString();
        }

        return new AdminCommonDaoImpl().updateAccountWhiteList(new WhiteAccount(id, platformUserId, remark)) ?
                AdminResult.defaultInstance().toJsonString() : AdminResult.failMsg("失败").toJsonString();
    }

    /**
     * 37.3 刷新
     */
    @ResponseBody
    @RequestMapping(value = "refresh", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String refresh(HttpServletRequest request, @RequestBody JSONObject param) {
        int channelId = param.getIntValue("channelId");

        if (eChannelType.forValue(channelId) == null) {
            return AdminResult.failMsg("channelId 错误").toJsonString();
        }

        AdminResult result = AdminResult.defaultInstance();
        List<ServerInfo> openServerList = ServerListMgr.getOpenServerList(channelId);
        String msg = "操作成功.";
        msg += CenterReloadHelper.serverMgrReload(openServerList, "PlatformUserIdMgr", 20);
        result.setMsg(msg);

        return result.toJsonString();
    }

    /**
     * 37.4 查询
     */
    @ResponseBody
    @RequestMapping(value = "query", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String query(HttpServletRequest request, @RequestBody JSONObject param) {
        String platformUserId = param.getString("openId"); // 模糊查询
        String remark = param.getString("remark"); // 模糊查询
        int pageNum = param.getIntValue("pageNum");
        int pageSize = param.getIntValue("pageSize");

        List<WhiteAccount> whiteAccounts = PlatformUserIdMgr.getWhiteAccounts();
        ArrayList<WhiteAccount> filteredData = new ArrayList<>(); // 筛选数据
        for (WhiteAccount account : whiteAccounts) {
            if (!StringUtils.isNullOrEmpty(platformUserId)) {
                String platformUserId1 = account.getPlatformUserId();
                if (StringUtils.isNullOrEmpty(platformUserId1) || !platformUserId1.contains(platformUserId)) {
                    continue;
                }
            }

            if (!StringUtils.isNullOrEmpty(remark)) {
                String remark1 = account.getRemark();
                if (StringUtils.isNullOrEmpty(remark1) || !remark1.contains(remark)) {
                    continue;
                }
            }

            filteredData.add(account);
        }

        int startIndex = (pageNum - 1) * pageSize;
        startIndex = Math.max(0, startIndex);

        JSONArray row = new JSONArray();
        for (int i = startIndex; i < startIndex + pageSize && i < filteredData.size(); i++) {
            WhiteAccount account = filteredData.get(i);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", account.getId());
            jsonObject.put("openId", account.getPlatformUserId());
            jsonObject.put("remark", account.getRemark());
            row.add(jsonObject);
        }
        AdminResult result = AdminResult.defaultInstance();
        result.getData().put("total", filteredData.size());
        result.getData().put("row", row);

        return result.toJsonString();
    }

    /**
     * 37.5 删除
     */
    @ResponseBody
    @RequestMapping(value = "delete", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String delete(HttpServletRequest request, @RequestBody JSONObject param) {
        int id = param.getIntValue("id");

        if (id == 0) {
            return AdminResult.failMsg("id值错误").toJsonString();
        }

        return new AdminCommonDaoImpl().deleteAccountWhiteList(id) ?
                AdminResult.defaultInstance().toJsonString() : AdminResult.failMsg("失败").toJsonString();
    }

    /**
     * 37.6 导入
     */
    @ResponseBody
    @RequestMapping(value = "batchImport", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String batchImport(HttpServletRequest request, @RequestBody JSONObject param) {
        JSONArray importData = param.getJSONArray("importData");

        List<WhiteAccount> accounts = new ArrayList<>();
        for (int i = 0; i < importData.size(); i++) {
            JSONObject jsonObject = importData.getJSONObject(i);
            String platformUserId = jsonObject.getString("openId");
            String remark = jsonObject.getString("remark");

            if (StringUtils.isNullOrEmpty(platformUserId)) {
                return AdminResult.failMsg("platformUserId 不能为空").toJsonString();
            }
            accounts.add(new WhiteAccount(platformUserId, remark));
        }

        return new AdminCommonDaoImpl().batchInsertAccountWhiteList(accounts) ?
                AdminResult.defaultInstance().toJsonString() : AdminResult.failMsg("失败").toJsonString();
    }
}
